#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
//#include <>

int main()
{
        int fd = open("log.txt",O_CREAT | O_RDONLY,0666);

        if(fd < 0)
        {
                perror("OPEN FAIL");
        }

        char buffer[1024] = { 0 };
        // read是系统调用，不是C语言库函数。
        // 所以read是不会提供最后末尾'\0'。
        ssize_t n = read(fd,buffer,sizeof(buffer) - 1);

	buffer[n] = 0;

	printf("%s\n",buffer);
	printf("%ld\n",n);

        close(fd);

	int code = 111;

	int fds[2] = { 0 };
	int ret = pipe(fds);

	if(ret < 0)
	{
		exit(1);
	}

	buffer[0] = 0;

	printf("code:%d\n",code);


	int send = 13;
	write(fds[1],&send,send);


	n = read(fds[0],&code,sizeof(code));

	buffer[n] = 0;

	printf("%s\n",buffer);
	printf("code1:%d\n",code);
	printf("n:%ld\n",n);


	send = 111;
	// 疑似每个pipe内存只能读一个同类型的数据。
	write(fds[1],&send,sizeof(send));

	code = 0;

	n = read(fds[0],&code,sizeof(code));

	printf("code2:%d\n",code);
	printf("send:%d\n",send);
	printf("n:%ld\n",n);

	send = 1231;
	code = 0;

	n = read(fds[0],&code,sizeof(code));

	printf("code3:%d\n",code);
	printf("send:%d\n",send);
	printf("n:%ld\n",n);

	close(fds[0]);
	close(fds[1]);



        return 0;
}

